<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Player_Action **/
require_once 'Zead/Player/Action.php';

/** @see Zead_Html **/
require_once 'Zead/Html.php';

/** @see Zend_Cache **/
require_once 'Zend/Cache.php';

/**
 * @category   Zead
 * @package    Zead_Player
 * @subpackage Zead_Player_Action
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License 
 */
class Zead_Player_Action_Default extends Zead_Player_Action 
{
	/**
	 * @var string
	 */
	protected $fileName = '';
	/**
	 * @var Zead_Html
	 */
	protected $html = null;
	
	/**
	 * @var string
	 */
	protected $htmlContent = '';
	
		
	/**
	 * Despachando a acao principal
	 */
	public function dispatch() 
	{
		$this->fileName = $this->getRequest()->getFileName();		
		$this->html = new Zead_Html();

		// Se naum vai utilizar o cache, entao habilita a renderizacao normal.
		if ((bool) $this->getPlayer()->getParam('useCache') == true) {
			$this->cacheRender();
		} else {
			$this->htmlRender();
		}				
		// Adicionando  parametro de sessao a todos os links.
		$this->html->setFilter('linkQuery', $this->getRequest()->getBaseQuery());
		$this->html->addFilter('stageDefault', $this->manifest->getDefaultPage());
		$this->html->formatTidy = true;

		$this->htmlContent = $this->html->saveHTML();	
		
		// Enviando o html para o browser.
		$this->getResponse()->setBody($this->htmlContent);		
	}
	
	/**
	 * Recuperando ou gravando o conteúdo modificado em cache.
	 * 
	 */
	protected function cacheRender()
	{		
		$cacheId = md5($this->fileName);
		// Opções do cache
		$frontendOptions = array(
			'lifetime' => 5, // Renovando o cache de 2 em 2 minutos 
			'automatic_serialization' => false
		);
		$backendOptions = array(
			'cache_dir' => $this->getPlayer()->getParam('cacheDir')
		);
		$cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);
			
		// Gerando a saída do cache, se não  houver nada gravado.
		if (!($this->htmlContent = $cache->load($cacheId))) {
			$this->htmlRender();
			$cache->save($this->htmlContent, $cacheId);				
		} 
		// Carregando o HTML do Cache
		$this->html->loadHTML($this->htmlContent);			
	}
	
	/**
	 * Renderizando opções básicas do HTML que podem ser estáticas
	 * 
	 */
	protected function htmlRender() 
	{	
		// Calculate the current pages range
		$pagesRange = array();
	
		$this->html->load($this->fileName);
		$this->html->headTitle($this->manifest->getTitle());
		$this->html->addFilter('heading', $this->manifest->getTitle());
		$this->html->addFilter('headingToc');
		$this->html->externalize();
		$this->html->linkPath($this->getRequest()->getScriptPath());
		
		// Applies filters to current content
		if ($this->manifest->hasCurrent()) {	
			$this->html->headTitle($this->manifest->getCurrentTitle(), 'append');
			$this->html->addFilter('heading', $this->manifest->getCurrentTitle());
			$this->html->addFilter('breadCrumb', $this->manifest->getCurrentParentHttpPagesTree());
			$this->html->addFilter('paginator', $this->manifest->getCurrentHttpPages(), $this->getRequest()->getScriptName());
		}
		// Applies the global navigation
		//$this->html->addFilter('tableContents', $this->manifest->getTocList());		
		$this->html->addFilter('menu', $this->manifest->getMenuList());
		$this->html->addFilter('acronym', $this->manifest->getObject()->getGlossaryList());					
		$this->html->addFilter('glossary', $this->manifest->getObject()->getGlossaryList());
		$this->html->addFilter('quiz', $this->manifest->getObject()->getQuizzesList()->toArray());

		// Salvando o HTML
		$this->htmlContent = $this->html->saveHTML();
	}
}
